package com.aliyun.heiyu.powermaster.server.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class ShellUtil {
    private static Logger logger = LoggerFactory.getLogger(ShellUtil.class);

    public static boolean  exec(String cmd){
        ProcessBuilder processBuilder = new ProcessBuilder();

        processBuilder.command("bash", "-c", cmd);
        try {
            Process process = processBuilder.start();
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                logger.info("call: " + cmd + " success!");
                logger.info(stringBuilder.toString());
                return true;
            } else {
                logger.error("call:" + cmd + " failed!");
                logger.info(stringBuilder.toString());
                return false;
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        }
        return false;
    }

    public static HashMap<String, String> exec1(String cmd){
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("bash", "-c", cmd);
        try {
            Process process = processBuilder.start();
            int exitVal = process.waitFor();
            HashMap<String, String> resultMap = new HashMap<>();
            if (exitVal == 0) {
                logger.info("call: " + cmd + " success!");
                StringBuilder stringBuilder = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] split = line.split(" ");
                    if(split.length > 1){
                        resultMap.put(split[0],split[1]);
                    }
                }
                logger.info("jieguowei "+resultMap);
                return resultMap;
            } else {
                logger.error("call:" + cmd + " failed!");
                return null;
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static String exec2(String cmd){
        ProcessBuilder processBuilder = new ProcessBuilder();

        processBuilder.command("bash", "-c", cmd);
        try {
            Process process = processBuilder.start();
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                logger.info("call: " + cmd + " success!");
                StringBuilder stringBuilder = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line + "\n");
                }
                logger.info("call: success ==  "+stringBuilder.toString());
                return stringBuilder.toString();
            } else {
                logger.error("call:" + cmd + " failed!");
                return null;
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static Map<String,String> exec4(String cmd){
        HashMap<String,String> map = new HashMap<>();
        ProcessBuilder processBuilder = new ProcessBuilder();

        processBuilder.command("bash", "-c", cmd);
        try {
            Process process = processBuilder.start();
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                logger.info("call: " + cmd + " success!");
                StringBuilder stringBuilder = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line + "\n");
                }
                logger.info("call: success ==  "+stringBuilder.toString());
                map.put("status","0");
                map.put("err",stringBuilder.toString());
                return map;
            } else {
                logger.error("call:" + cmd + " failed!");
                StringBuilder stringBuilder = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line + "\n");
                }
                map.put("status","1");
                map.put("err",stringBuilder.toString());
                return map;
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        }
        map.put("status","1");
        map.put("err","操作失败，请重试");
        return map;
    }

}
